package com.czwj.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.entities.DataGridData;
import com.czwj.entities.Equipment;
import com.czwj.entities.PageBean;
import com.czwj.entities.Pressure;
import com.czwj.service.EquipmentService;
import com.czwj.service.PressureService;
import com.czwj.util.TextUtil;

import net.sf.json.JSONObject;

@Controller
public class PressureAction {

	@Resource
	PressureService pressureService;
	@Resource
	EquipmentService equipmentService;
	
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@RequestMapping(value = "/PressureList",method = RequestMethod.GET)
	@ResponseBody
	public List<Pressure> list(@RequestParam(value = "equipmentid",required = true) Integer equipmentid,
			@RequestParam(value = "startdate",required = false) String startdate,
			@RequestParam(value = "enddate",required = false) String enddate) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("equipmentid", equipmentid);
		if(StringUtils.isNotEmpty(startdate)) {
			map.put("startdate", startdate);
		}
		if(StringUtils.isNotEmpty(enddate)) {
			map.put("enddate", enddate);
		}
		return pressureService.search(map);
	}
	@RequestMapping(value = "/NewestPressure",method = RequestMethod.GET)
	@ResponseBody
	public Pressure newest(@RequestParam(value = "equipmentid",required = true) Integer equipmentid) {
		return pressureService.findNewest(equipmentid);
	}
	
	@RequestMapping(value = "/NewestPressureByProject",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Equipment> newestPressureByProject(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = true) Integer projectitemid) {
		DataGridData<Equipment> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		map.put("projectitemid",projectitemid);
		List<Equipment> list = equipmentService.searchWithPressure(map);
		result.setRows(list);
		result.setTotal(equipmentService.count(map));
		return result;
	}
	@RequestMapping(value = "/NewestPressureBySingle",method = RequestMethod.GET)
	@ResponseBody
	public Equipment NewestPressureBySingle(@RequestParam(value = "equipmentid",required = true) Integer id) {
		
		return equipmentService.selectWithPressureByPrimaryKey(id);
	}
	
	
	@RequestMapping(value = "/PressureCirlce",method = RequestMethod.GET)
	@ResponseBody
	public List<List<Pressure>> PressureCirlce(@RequestParam(value = "equipmentid",required = true) Integer equipmentid,
			@RequestParam(value = "date",required = true) String date) {
		List<List<Pressure>> result = new ArrayList<>();
 		Map<String, Object> map = new HashMap<String, Object>();
		map.put("equipmentid", equipmentid);
 		map.put("date", date);
		List<Pressure> stopWorking = pressureService.getStopWorkingTimes(map);
		if(CollectionUtils.isEmpty(stopWorking)) {
			map.put("startdate", date);
			map.put("enddate", date);
			List<Pressure> list = pressureService.search(map);
			result.add(list);
			return result;
		}
		map.put("id", stopWorking.get(0).getId());
		Pressure lastStoping = pressureService.getLastStoping(map);
		for (int i=0;i<stopWorking.size();i++) {
			if(i==0) {
				if(lastStoping!=null) {
					map.put("startid", lastStoping.getId());
				}
				map.put("endid", stopWorking.get(0).getId());
			}else {
				map.put("startid", stopWorking.get(i-1).getId());
				map.put("endid", stopWorking.get(i).getId());
			}
			List<Pressure> row = pressureService.getWorkingCircle(map);
			result.add(row);
		}
		map.put("startid", stopWorking.get(stopWorking.size()-1).getId());
		map.remove("endid");
		List<Pressure> row = pressureService.getWorkingCircle(map);
		result.add(row);
		return result;
	}
	
	@RequestMapping(value = "/PressureSubmit",method = RequestMethod.POST,produces="text/plain;charset=ISO-8859-1")
	@ResponseBody
	public JSONObject PressureSubmit(@RequestHeader("header") String header,
			@RequestParam("data") String data) {
		JSONObject result = new JSONObject();
		System.out.println("header:"+header);
		System.out.println("data:"+data);
		if(!header.equals("CZDD")) {
			result.put("msg", "header错误");
			return result;
		}
		try {
			String imei = data.substring(3, 18);
			Equipment equipment = equipmentService.findByCode(imei);
			if(equipment==null) {
				result.put("msg", "数据错误");
				return result;
			}
			Integer equipmentid = -1;
			equipmentid = equipment.getId();
			Date date = sdf.parse(data.substring(18, 30));
			System.out.println(sdf2.format(date));
			String workstatus = data.substring(30, 31);
			System.out.println(workstatus);
			Integer timeblank = Integer.parseInt(data.substring(31, 35));
			System.out.println(timeblank);
			String chipstatus = data.substring(35, 36);
			System.out.println(chipstatus);
			String rotatestatus = data.substring(36, 37);
			System.out.println(rotatestatus);
			Double rotate = Double.valueOf(data.substring(37, 41)) / 2000 * 40;
			System.out.println(rotate);

			String pushstatus = data.substring(41, 42);
			System.out.println(pushstatus);
			Double push = Double.valueOf(data.substring(42, 46)) / 2000 * 40;
			System.out.println(push);

			String mudstatus = data.substring(46, 47);
			System.out.println(mudstatus);
			Double mud = Double.valueOf(data.substring(47, 51)) / 2000 * 40;
			System.out.println(mud);

			String gpsstatus = data.substring(51, 52);
			System.out.println(gpsstatus);
			Double latitude = 0d;
			Double longitude = 0d;
			if(!data.substring(52, 53).equals("0")) {
				latitude = (data.substring(52, 53).equals("N") ? 1 : -1)
						* (Double.valueOf(data.substring(53, 61))) / 1000000;
			}
			if(!data.substring(61, 62).equals("0")) {
				longitude = (data.substring(61, 62).equals("E") ? 1 : -1)
						* (Double.valueOf(data.substring(62, 71))) / 1000000;
			}
			System.out.println(latitude);
			System.out.println(longitude);
			Double height = (data.substring(71, 72).equals("+") ? 1 : -1)
					* (Double.valueOf(data.substring(72, 77))) / 10;
			System.out.println(height);
			Double speed = Double.valueOf(data.substring(77, 81)) / 10;
			System.out.println(speed);
			Integer version = Integer.parseInt(data.substring(81, 82));
			System.out.println(version);
			Pressure pressure = new Pressure(imei, equipmentid, rotatestatus, rotate, pushstatus, push,
					mudstatus, mud, date, workstatus, timeblank, chipstatus, gpsstatus, longitude, latitude,
					height, speed, version);
			pressureService.insertSelective(pressure);
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("msg", "数据错误");
			return result;
		}
		
	}
}
